/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.controller;

import com.techshop.model.Color;
import com.techshop.model.District;
import com.techshop.model.Phone;
import com.techshop.model.PhoneCode;
import com.techshop.model.PhoneCodeStore;
import com.techshop.model.PhoneCodeStoreId;
import com.techshop.model.Province;
import com.techshop.model.ReceiptNote;
import com.techshop.model.ReceiptNoteDetail;
import com.techshop.model.Store;
import com.techshop.service.ColorService;
import com.techshop.service.DistrictService;
import com.techshop.service.PhoneCodeService;
import com.techshop.service.PhoneCodeStoreService;
import com.techshop.service.PhoneService;
import com.techshop.service.ProvinceService;
import com.techshop.service.ReceiptNoteDetailService;
import com.techshop.service.ReceiptNoteService;
import com.techshop.service.StoreService;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author Hai-PC
 */
@ManagedBean(name = "receiptNoteController")
@ViewScoped
public class ReceiptNoteController extends AbstractController
{
    private ReceiptNote selectedReceiptNote;
    private ReceiptNoteDetail selectedReceiptNoteDetails;
    private Province selectedProvince;
    private District selectedDistrict;
    private Phone selectedPhone;
    private Color selectedColor;
    private Store selectedStore;
    
    @ManagedProperty(value = "#{receiptNoteService}")
    private ReceiptNoteService receiptNoteService;
    @ManagedProperty(value = "#{receiptNoteDetailService}")
    private ReceiptNoteDetailService receiptNoteDetailService;
    @ManagedProperty(value = "#{provinceService}")
    private ProvinceService provinceService;
    @ManagedProperty(value = "#{districtService}")
    private DistrictService districtService;
    @ManagedProperty(value = "#{phoneService}")
    private PhoneService phoneService;
    @ManagedProperty(value = "#{colorService}")
    private ColorService colorService;
    @ManagedProperty(value = "#{storeService}")
    private StoreService storeService;
    @ManagedProperty(value = "#{phoneCodeStoreService}")
    private PhoneCodeStoreService phoneCodeStoreService;
    @ManagedProperty(value = "#{phoneCodeService}")
    private PhoneCodeService phoneCodeService;
    
    private List<Province> provinceList;
    private List<District> districtList;
    private List<Phone> phoneList;
    private List<PhoneCode> colorList;
    private List<Store> storeList;
    private List<ReceiptNoteDetail> tempList = new ArrayList();
    
    @PostConstruct
    public void init()
    {
        resetSelectedReceiptNote();
    }
    
    public void resetSelectedReceiptNote()
    {
        selectedReceiptNote = new ReceiptNote();    
        selectedProvince = new Province();
        selectedDistrict = new District();
        selectedPhone = new Phone();
        selectedColor = new Color();
        selectedStore = new Store();
        selectedReceiptNote.setDate(Calendar.getInstance().getTime());
        selectedReceiptNote.setTotal(0);
        if (!tempList.isEmpty())
        {
            tempList.clear();
        }
        resetSelectedReceiptNoteDetails();
    }
    
    public void resetSelectedReceiptNoteDetails()
    {
        selectedReceiptNoteDetails = new ReceiptNoteDetail();
        selectedReceiptNoteDetails.setQuantity(0);
        selectedReceiptNoteDetails.setPrice(0);
    }
    
    public void createReceiptNoteDetails()
    {
        selectedReceiptNoteDetails.setPhone(phoneService.getById(selectedPhone.getId()));
        selectedReceiptNoteDetails.setColor(colorService.getById(selectedColor.getId()));
        for (ReceiptNoteDetail r : tempList)
        {
            if (r.getPhone().getId() == selectedReceiptNoteDetails.getPhone().getId() && r.getColor().getId().equals(selectedReceiptNoteDetails.getColor().getId()))
            {
                addErrorMessage("Sản phẩm đã tồn tại");
                return;
            }
        }
        if (tempList.add(selectedReceiptNoteDetails))
        {
            selectedReceiptNote.setTotal(selectedReceiptNote.getTotal() + selectedReceiptNoteDetails.getPrice() * selectedReceiptNoteDetails.getQuantity());
            resetSelectedReceiptNoteDetails();
        }
    }
    
    public void deleteReceiptNoteDetails(int phoneId, String colorId)
    {
        for (ReceiptNoteDetail r : tempList)
        {
            if (r.getPhone().getId() == phoneId && r.getColor().getId().equals(colorId))
            {
                tempList.remove(r);
                selectedReceiptNote.setTotal(selectedReceiptNote.getTotal() - r.getPrice() * r.getQuantity());
                return;
            }
        }
    }
    
    public void insertReceiptNote()
    {
        selectedReceiptNote.setStore(selectedStore);
        if (receiptNoteService.insert(selectedReceiptNote))
        {
            for (ReceiptNoteDetail r : tempList)
            {
                r.setReceiptNote(selectedReceiptNote);
                receiptNoteDetailService.insert(r);
                PhoneCodeStore phoneCodeStore = phoneCodeStoreService.getByPhoneCodeAndStore(phoneCodeService.getByPhoneAndColor(r.getPhone().getId(), r.getColor().getId()).getId(), selectedReceiptNote.getStore().getId());
                if (phoneCodeStore == null) // Add new
                {
                    phoneCodeStore = new PhoneCodeStore();
                    PhoneCode phoneCode = phoneCodeService.getByPhoneAndColor(r.getPhone().getId(), r.getColor().getId());
                    phoneCodeStore.setPhoneCode(phoneCode);
                    phoneCodeStore.setId(new PhoneCodeStoreId(phoneCode.getId(), selectedStore.getId()));
                    phoneCodeStore.setStore(selectedStore);
                    phoneCodeStore.setQuantity(r.getQuantity());
                    phoneCodeStore.setProvince(new Province());
                    phoneCodeStore.getProvince().setId(selectedProvince.getId());
                    phoneCodeStore.setDistrict(new District());
                    phoneCodeStore.getDistrict().setId(selectedDistrict.getId());
                    phoneCodeStore.setPhone(new Phone());
                    phoneCodeStore.getPhone().setId(selectedPhone.getId());
                    phoneCodeStoreService.insert(phoneCodeStore);
                }
                else
                {
                    phoneCodeStore.setQuantity(phoneCodeStore.getQuantity() + r.getQuantity());
                    phoneCodeStoreService.update(phoneCodeStore);
                }
                // Update Phone and PhoneCode
                phoneService.updateQuantity(r.getPhone().getId(), r.getQuantity());
                
                PhoneCode phoneCode = phoneCodeService.getByPhoneAndColor(r.getPhone().getId(), r.getColor().getId());
                phoneCode.setQuantity(phoneCode.getQuantity() + r.getQuantity());
                phoneCodeService.update(phoneCode);
            }
            addInfoMessage("Nhập hàng thành công");
            resetSelectedReceiptNote();
        }
        else
        {
            addErrorMessage("Nhập hàng thất bại");
        }
    }
    
    public ReceiptNote getSelectedReceiptNote()
    {
        return selectedReceiptNote;
    }

    public void setSelectedReceiptNote(ReceiptNote selectedReceiptNote)
    {
        this.selectedReceiptNote = selectedReceiptNote;
    }
    
    public ReceiptNoteService getReceiptNoteService()
    {
        return receiptNoteService;
    }

    public void setReceiptNoteService(ReceiptNoteService receiptNoteService)
    {
        this.receiptNoteService = receiptNoteService;
    }

    public Province getSelectedProvince() {
        return selectedProvince;
    }

    public void setSelectedProvince(Province selectedProvince) {
        this.selectedProvince = selectedProvince;
    }

    public District getSelectedDistrict() {
        return selectedDistrict;
    }

    public void setSelectedDistrict(District selectedDistrict) {
        this.selectedDistrict = selectedDistrict;
    }

    public Phone getSelectedPhone() {
        return selectedPhone;
    }

    public void setSelectedPhone(Phone selectedPhone) {
        this.selectedPhone = selectedPhone;
    }

    public Color getSelectedColor() {
        return selectedColor;
    }

    public void setSelectedColor(Color selectedColor) {
        this.selectedColor = selectedColor;
    }

    public Store getSelectedStore() {
        return selectedStore;
    }

    public void setSelectedStore(Store selectedStore) {
        this.selectedStore = selectedStore;
    }


    public ProvinceService getProvinceService() {
        return provinceService;
    }

    public void setProvinceService(ProvinceService provinceService) {
        this.provinceService = provinceService;
    }

    public DistrictService getDistrictService() {
        return districtService;
    }

    public void setDistrictService(DistrictService districtService) {
        this.districtService = districtService;
    }

    public PhoneService getPhoneService() {
        return phoneService;
    }

    public void setPhoneService(PhoneService phoneService) {
        this.phoneService = phoneService;
    }

    public ColorService getColorService() {
        return colorService;
    }

    public void setColorService(ColorService colorService) {
        this.colorService = colorService;
    }

    public StoreService getStoreService() {
        return storeService;
    }

    public void setStoreService(StoreService storeService) {
        this.storeService = storeService;
    }

    public List<Province> getProvinceList() {
        setProvinceList(provinceService.getAll());
        return provinceList;
    }

    public void setProvinceList(List<Province> provinceList) {
        this.provinceList = provinceList;
    }

    public List<District> getDistrictList() {
        setDistrictList(districtService.getByProvinceId(selectedProvince.getId()));
        return districtList;
    }

    public void setDistrictList(List<District> districtList) {
        this.districtList = districtList;
    }

    public List<Phone> getPhoneList() {
        setPhoneList(phoneService.getAll());
        return phoneList;
    }

    public void setPhoneList(List<Phone> phoneList) {
        this.phoneList = phoneList;
    }

    public List<PhoneCode> getPhoneCodeList() {
        try
        {
            setPhoneCodeList(phoneCodeService.getByPhoneId(selectedPhone.getId()));
        }
        catch (NullPointerException e)
        {
        }
        return colorList;
    }

    public void setPhoneCodeList(List<PhoneCode> colorList) {
        this.colorList = colorList;
    }

    public List<Store> getStoreList() {
        setStoreList(storeService.getByProvinceAndDistrict(selectedProvince.getId(), selectedDistrict.getId()));
        return storeList;
    }

    public void setStoreList(List<Store> storeList) {
        this.storeList = storeList;
    }

    public ReceiptNoteDetail getSelectedReceiptNoteDetails() {
        return selectedReceiptNoteDetails;
    }

    public void setSelectedReceiptNoteDetails(ReceiptNoteDetail selectedReceiptNoteDetails) {
        this.selectedReceiptNoteDetails = selectedReceiptNoteDetails;
    }

    public List<ReceiptNoteDetail> getTempList() {
        return tempList;
    }

    public void setTempList(List<ReceiptNoteDetail> tempList) {
        this.tempList = tempList;
    }

    public ReceiptNoteDetailService getReceiptNoteDetailService() {
        return receiptNoteDetailService;
    }

    public void setReceiptNoteDetailService(ReceiptNoteDetailService receiptNoteDetailService) {
        this.receiptNoteDetailService = receiptNoteDetailService;
    }

    public PhoneCodeStoreService getPhoneCodeStoreService() {
        return phoneCodeStoreService;
    }

    public void setPhoneCodeStoreService(PhoneCodeStoreService phoneCodeStoreService) {
        this.phoneCodeStoreService = phoneCodeStoreService;
    }

    public PhoneCodeService getPhoneCodeService() {
        return phoneCodeService;
    }

    public void setPhoneCodeService(PhoneCodeService phoneCodeService) {
        this.phoneCodeService = phoneCodeService;
    }
    
    
}